{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import glob\n",
    "import numpy as np\n",
    "import math\n",
    "from matplotlib import pyplot as plt\n",
    "import matplotlib\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "img_left = cv2.imread(r'C:/Users/HuangSX/Desktop/OpenCV/data/featureMat_left.jpg')\n",
    "img_right = cv2.imread(r'C:/Users/HuangSX/Desktop/OpenCV/data/featureMat_right.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sift算子进行特征匹配\n",
    "def sift(img1, img2):\n",
    "    gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)\n",
    "    gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)\n",
    "\n",
    "    maxcorners = 1000\n",
    "\n",
    "    sift = cv2.xfeatures2d_SIFT.create(nfeatures=maxcorners)\n",
    "    kp1, descriptors1 = sift.detectAndCompute(gray1,None)\n",
    "    kp2, descriptors2 = sift.detectAndCompute(gray2,None)\n",
    "\n",
    "    bfMatcher = cv2.FlannBasedMatcher()\n",
    "    matches = bfMatcher.knnMatch(descriptors1, descriptors2, k=2)\n",
    "\n",
    "    good = [[m] for m, n in matches if m.distance < 0.4 * n.distance]\n",
    "    img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, None, flags=2)\n",
    "\n",
    "    #plt.imshow(img3)\n",
    "    #cv2.waitKey(1000)\n",
    "    cv2.imwrite(r\"C:/Users/HuangSX/Desktop/OpenCV/data/sift_matching.jpg\", img3)\n",
    "    #cv2.destroyAllWindows()\n",
    "    print(\"finish sift algrithm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# SURF算子进行特征匹配\n",
    "def SURF_detect(img1, img2):\n",
    "    gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)\n",
    "    gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)\n",
    "\n",
    "    minThreashold = 1000  # hession 矩阵阈值，在这里调整精度，值越大点越少，越精准\n",
    "    surf = cv2.xfeatures2d_SURF.create(minThreashold)\n",
    "    keypoints, descriptor = surf.detectAndCompute(gray1, None)\n",
    "    keypoints2, descriptor2 = surf.detectAndCompute(gray2, None)\n",
    "\n",
    "    bfMatcher = cv2.FlannBasedMatcher()\n",
    "    matches = bfMatcher.knnMatch(descriptor, descriptor2, k=2)\n",
    "\n",
    "    good = [[m] for m, n in matches if m.distance < 0.5 * n.distance]\n",
    "    img3 = cv2.drawMatchesKnn(img1, keypoints, img2, keypoints2, good, None, flags=2)\n",
    "\n",
    "    #plt.imshow(img3)\n",
    "    #cv2.waitKey(1000)\n",
    "    cv2.imwrite(r\"C:/Users/HuangSX/Desktop/OpenCV/data/surf_matching.jpg\", img3)\n",
    "    #cv2.destroyAllWindows()\n",
    "    print(\"finish surf algrithm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ORB算子进行特征匹配\n",
    "def ORB_detect(img1, img2):\n",
    "    gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)\n",
    "    gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)\n",
    "\n",
    "    nkeypoint = 100  # 算法在图片中找到匹配点的对数\n",
    "\n",
    "    orb = cv2.ORB.create(nkeypoint)\n",
    "\n",
    "    kp1, descriptors1 = orb.detectAndCompute(gray1, None)\n",
    "    kp2, descriptors2 = orb.detectAndCompute(gray2, None)\n",
    "\n",
    "    bf = cv2.DescriptorMatcher.create('BruteForce')\n",
    "    matches = bf.match(descriptors1, descriptors2)\n",
    "\n",
    "\n",
    "    img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches, None, flags=2)\n",
    "\n",
    "    #plt.imshow(img3)\n",
    "    #cv2.waitKey(1000)\n",
    "    cv2.imwrite(r\"C:/Users/HuangSX/Desktop/OpenCV/data/orb_matching.jpg\", img3)\n",
    "    #cv2.destroyAllWindows()\n",
    "    print(\"finish orb algrithm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "finish sift algrithm\n",
      "finish surf algrithm\n",
      "finish orb algrithm\n"
     ]
    }
   ],
   "source": [
    "sift(img_left, img_right)\n",
    "SURF_detect(img_left, img_right)\n",
    "ORB_detect(img_left, img_right)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
